import os
from flask import Flask
from flask_session import Session
from datetime import timedelta

def create_app(config_name=None):
    app = Flask(__name__)
    
    # 基本配置
    app.config['SECRET_KEY'] = os.urandom(24)
    app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'uploads')
    app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024  # 1GB max-limit
    
    # Session配置
    app.config['SESSION_TYPE'] = 'filesystem'
    app.config['SESSION_FILE_DIR'] = os.path.join(os.getcwd(), 'sessions')
    app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=30)
    Session(app)
    
    # 注册蓝图
    from .api.auth import auth_bp
    from .api.upload import upload_bp
    from .api.download import download_bp
    from .api.system import system_bp
    
    # 注册 auth_bp 到根路径
    app.register_blueprint(auth_bp, url_prefix='/')
    app.register_blueprint(upload_bp, url_prefix='/upload')
    app.register_blueprint(download_bp, url_prefix='/documents')
    app.register_blueprint(system_bp, url_prefix='/system')
    
    # 确保上传目录存在
    os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
    
    return app 